//app.js
import Ajax from './utils/request.js'

App({
  globalData: {
    userInfo: null,
    encryptedData: null,
    userAuthData: {},
    phone: null,
    appCode: '',
    isRealNameBool: 0
  },
  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    this.getAuthEvent();
    this.getSystemInfoEvent();
    // 登录
    wx.login({
      success: res => {
        this.globalData.appCode = res.code;
        console.log(this.globalData.appCode)
        this.getWxLoginEvent(res.code);
      }
    })
  },
  async getWxLoginEvent(code) {
    try {
      let res = await Ajax.get('/wxMiniProgram/wxLogin', {
        code
      });
      let data = res.result;
      this.globalData.phone = data.phone;
      wx.setStorageSync('wxAccessToken', data.wxAccessToken);
      console.log(res)
    } catch (error) {
      console.log(error)
    }
  },
  // 小程序获取授权信息
  getAuthEvent() {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
          wx.redirectTo({
            url: '/pages/authorization/authorization',
          })
        }
      }
    })
  },
  // 获取系统的基本信息
  getSystemInfoEvent() {
    let _this = this;
    wx.getSystemInfo({
      success(res) {
        _this.globalData.statusBarHeight = res.statusBarHeight;
        const model = res.model;
        const modelInclude = ["iPhone X", 'iPhone XR', "iPhone XS", "iPhone XS MAX"];
        var flag = false; //是否X以上机型
        for (let i = 0; i < modelInclude.length; i++) {
          //模糊判断是否是modelInclude 中的机型,因为真机上测试显示的model机型信息比较长无法一一精确匹配
          if (model.indexOf(modelInclude[i]) != -1) {
            flag = true
          }
        }
        if (flag) {
          //如果是这几个机型，设置距离底部的bottom值
          _this.globalData.bottom = 25;
        }
      }
    })
  },
})